package com.ehotting.mqbd.core.helper;

import com.ehotting.eaf.core.utils.StringUtils;
import com.ehotting.mqbd.api.bean.MQDataAble;
import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.api.enums.DirectionEnum;
import com.ehotting.mqbd.api.enums.SolveStepEnum;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.core.mq.consumer.action.ConsumerActionAble;



public class MQStringHelper {

    private static String SPLIT = "_";
    private static String IP_UNDER_LINE = "-";

    public static String createTopic(TopicEnum topic,String module,DirectionEnum direction){
        String preName = "TOPIC";
        StringBuffer buf = new StringBuffer();
        buf.append(preName).append(SPLIT);
        buf.append(topic.getCode()).append(SPLIT);
        buf.append(module).append(SPLIT);
        buf.append(direction.getCode());
        return buf.toString().replaceAll("\\.",IP_UNDER_LINE);
    }

    public static String createProducerTopic(TopicEnum topic,MQDataAble data){
        String preName = "TOPIC";
        StringBuffer buf = new StringBuffer();
        buf.append(preName).append(SPLIT);
        buf.append(topic.getCode()).append(SPLIT);
        buf.append(data.getModule()).append(SPLIT);
        buf.append(data.getDirection());

        return buf.toString().replaceAll("\\.",IP_UNDER_LINE);
    }

    public static String createConsumerTopic(ConsumerActionAble consumerAction){

        String preName = "TOPIC";
        StringBuffer buf = new StringBuffer();
        buf.append(preName).append(SPLIT);
        buf.append(consumerAction.getTopic().getCode()).append(SPLIT);
        buf.append(consumerAction.getModule()).append(SPLIT);
        buf.append(consumerAction.getDirection().getCode());

        return buf.toString().replaceAll("\\.",IP_UNDER_LINE);
    }



    public static String createProducerTag(MQDataAble data){
        String preName = "TAG";
        StringBuffer buf = new StringBuffer();
        buf.append(preName).append(SPLIT);
        buf.append(data.getConsumerHost()).append(SPLIT);
        buf.append(data.getModule()).append(SPLIT);
        buf.append(data.getDirection());

        if(StringUtils.hasText(data.getSolverIp()) && data.getSolveStep() == SolveStepEnum.MULTI){
            buf.append(SPLIT).append(data.getSolverIp());
        }

        return buf.toString().replaceAll("\\.",IP_UNDER_LINE);
    }

    public static String createConsumerTag(ConsumerActionAble consumerAction){
        String preName = "TAG";
        StringBuffer buf = new StringBuffer();
        buf.append(preName).append(SPLIT);
        buf.append(consumerAction.getHost()).append(SPLIT);
        buf.append(consumerAction.getModule()).append(SPLIT);
        buf.append(consumerAction.getDirection().getCode());

        if(StringUtils.hasText(consumerAction.getSolverIp()) ){
            buf.append(SPLIT).append(consumerAction.getSolverIp());
        }

        return buf.toString().replaceAll("\\.",IP_UNDER_LINE);
    }


    public static String createConsumerGroup(TopicEnum topic, String module,String consumerHost, DirectionEnum direction){
        String preName = "CONSUMER";
        return (preName+SPLIT+
                topic.getCode()+SPLIT+
                module+SPLIT+
                consumerHost+SPLIT+
                direction.getCode()).replaceAll("\\.",IP_UNDER_LINE);
    }


    public static String createConsumerGroupAtConsumerApp(TopicEnum topic, MqApplicationContext mqApplicationContext, DirectionEnum direction){
        return createConsumerGroupAtConsumerApp( topic,  mqApplicationContext,  direction, null);
    }

    /**
     * 在客户端调用，生成消费客户端consumerGroup
     * @param topic
     * @param mqApplicationContext
     * @param direction
     * @return
     */
    public static String createConsumerGroupAtProducerApp(TopicEnum topic, MqApplicationContext mqApplicationContext, DirectionEnum direction){
        return createConsumerGroupAtProducerApp(topic, mqApplicationContext,  direction, null);
    }

    public static String createConsumerGroupAtConsumerApp(TopicEnum topic, MqApplicationContext mqApplicationContext, DirectionEnum direction,String solverIp){
        String preName = "CONSUMER";
        String consumerGroup = preName+SPLIT+
                topic.getCode()+SPLIT+
                mqApplicationContext.getModule()+SPLIT+
                mqApplicationContext.getLocalHost()+SPLIT+
                direction.getCode();
        if(StringUtils.hasText(solverIp)){
            consumerGroup+=SPLIT;
            consumerGroup+=solverIp;
        }
        return consumerGroup.replaceAll("\\.",IP_UNDER_LINE);
    }

    /***
     * 在客户端调用，生成消费客户端consumerGroup
     * @param topic
     * @param mqApplicationContext
     * @param direction
     * @param solverIp
     * @return
     */
    public static String createConsumerGroupAtProducerApp(TopicEnum topic, MqApplicationContext mqApplicationContext, DirectionEnum direction,String solverIp){
        String preName = "CONSUMER";
        String consumerGroup = preName+SPLIT+
                topic.getCode()+SPLIT+
                mqApplicationContext.getModule()+SPLIT+
                mqApplicationContext.getConsumerHost()+SPLIT+
                direction.getCode();
        if(StringUtils.hasText(solverIp)){
            consumerGroup+=SPLIT;
            consumerGroup+=solverIp;
        }
        return consumerGroup.replaceAll("\\.",IP_UNDER_LINE);
    }

    public static String createProducerGroup(MqApplicationContext mqApplicationContext){
        String preName = "PRODUCER";
        return (preName+SPLIT+ mqApplicationContext.getModule()+ SPLIT+ mqApplicationContext.getLocalHost()).replaceAll("\\.",IP_UNDER_LINE);
    }


    public static String createProducerGroup(String localHost){
        String preName = "PRODUCER";
        return (preName+SPLIT+ localHost).replaceAll("\\.",IP_UNDER_LINE);
    }



}
